package boogui.admin;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import boogui.db.BooguiDAO;
import boogui.db.IBooguiDAO;
import boogui.db.MovieRegistrySQL;
import boogui.movie.MovieVO;

public class ShowingRegistryDAO implements IShowingRegistryDAO {
	private MovieRegistrySQL sql = null;
	private IBooguiDAO bdao = null;

	public ShowingRegistryDAO() {
		sql = new MovieRegistrySQL();
		bdao = new BooguiDAO();
	}

	// 모든 지역
	@Override
	public List<MovieVO> allLocation() {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<MovieVO> result = new ArrayList<>();

		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(sql.getAllLocationSQL());
			rs = pstmt.executeQuery();

			while (rs.next()) {
				MovieVO m = new MovieVO();
				m.setLocation(rs.getString("location"));
				result.add(m);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, rs);
		}
		return result;
	}

	// 해당 지역 영화관 이름
	@Override
	public List<MovieVO> locationCinema(String list) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<MovieVO> result = new ArrayList<>();

		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(sql.getCinemaSQL());
			pstmt.setString(1, list);
			rs = pstmt.executeQuery();

			while (rs.next()) {
				MovieVO mv = new MovieVO();
				mv.setCinename(rs.getString("cinename"));
				result.add(mv);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, rs);
		}
		return result;
	}

	// 해당 영화관의 상영관
	@Override
	public List<MovieVO> cinemaTheater(String list) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<MovieVO> result = new ArrayList<>();

		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(sql.getTheaterSQL());
			pstmt.setString(1, list);
			rs = pstmt.executeQuery();

			while (rs.next()) {
				MovieVO mv = new MovieVO();
				mv.setTheater(rs.getString("theater"));
				result.add(mv);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, rs);
		}
		return result;
	}

	// 영화 제목 조회 (전체)
	@Override
	public List<String> selectMovieTitle() {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<String> result = new ArrayList<>();
		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(sql.getSelectMovieSQL());
			rs = pstmt.executeQuery();
			while (rs.next()) {
				result.add(rs.getString("title"));
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, rs);
		}
		return result;
	}

	// 상영 정보 등록
	@Override
	public boolean insertShowing(MovieVO m) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		boolean result = false;
		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(sql.getInsertShowingSQL());			
			pstmt.setString(1, m.getMovtitle());
			pstmt.setString(2, m.getLocation());
			pstmt.setString(3, m.getCinename());
			pstmt.setString(4, m.getTheater());
			pstmt.setString(5, m.getStime());
			pstmt.setString(6, m.getSdate());			
			pstmt.executeUpdate();
			pstmt.close();
			
			pstmt = conn.prepareStatement(sql.getIsPlaySQL());
			pstmt.setString(1, m.getMovtitle());
			pstmt.executeUpdate();
			pstmt.close();			

			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, null);
		}
		return result;
	}

	@Override
	public String selectShowNo() {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String result = ""; 
		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(sql.getSelectShownoSQL());					
			rs = pstmt.executeQuery();
			
			while (rs.next()){
				result = rs.getString("showno");
			}			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, rs);
		}
		
		return result;
	}

	@Override
	public boolean insertSeat(String seatsql) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		boolean result = false;
		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(seatsql);
			pstmt.executeUpdate();
			
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, null);
		}
		return result;
	}

}
